package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;

import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @author hljstart
 * @create 2022-05-26-9:46
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController{
    @Reference
    private AdminService adminService;

    @Reference
    private RoleService roleService;

    //注入密码加密器
    @Autowired
    private PasswordEncoder passwordEncoder;
    //分页及带条件查询
    @RequestMapping
    public String findPage(Map map, HttpServletRequest request){
        //获取请求参数
        Map<String, Object> filters = getFilters(request);
        //将filters放入到request域中
        map.put("filters",filters);
        //调用AdminService中分页的方法
        PageInfo<Admin> pageInfo = adminService.findPage(filters);
        //将pageInfo对象放到request域中
        map.put("page",pageInfo);

        return "admin/index";
    }
    //去添加用户的页面
    @RequestMapping("/create")
    public String goAddPage(){
        return "admin/create";
    }
    //新增用户
    @RequestMapping("/save")
    public String save(Admin admin){
        //对Admin对象中的密码进行加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return "common/successPage";
    }
    //删除用户
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        adminService.delete(id);
        //重定向
        return "redirect:/admin";
    }
    //去更新的页面
    @RequestMapping("/edit/{id}")
    public String goEditPage(@PathVariable("id")Long id,Map map){
        //调用AdminService中根据id查询用户的方法
        Admin admin = adminService.getById(id);
        map.put("admin",admin);
        return "admin/edit";
    }

    //根据用户id修改用户信息
    @RequestMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    /**
     * 上传图片
     * @param map
     * @param id
     * @return
     */
    @GetMapping("/uploadShow/{id}")
    public String uploadShow(Map map,@PathVariable("id") Long id) {
        map.put("id",id);
        return "admin/upload";
    }


    @PostMapping("/upload/{id}")
    public String upload(@PathVariable("id") Long id, @RequestParam("file")MultipartFile file)  {
        try {
            Admin admin = adminService.getById(id);
            byte[] bytes = file.getBytes();
            String fileName = UUID.randomUUID().toString();
            QiniuUtils.upload2Qiniu(bytes,fileName);
            admin.setHeadUrl("http://rcxr7c1gi.hn-bkt.clouddn.com/"+fileName);
            adminService.update(admin);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "common/successPage";
    }

    //去跟配角色的页面
    @RequestMapping("/assignShow/{adminId}")
    public String goAssignShowPage(@PathVariable("adminId")Long adminId, ModelMap modelMap){
        //将用户id放入到requeste域中
        modelMap.addAttribute("adminId",adminId);
        //调用roleService中根据id查询用户的角色
        Map<String, Object> rolesByAdminId = roleService.findRolesByAdminId(adminId);
        //将map放到request域中
        modelMap.addAllAttributes(rolesByAdminId);
        return "admin/assignShow";
    }

    //分配角色
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId,Long[] roleIds){
        //调用RoleService中分配角色的方法
        roleService.assignRole(adminId,roleIds);
        return "common/successPage";
    }
}
